package com.ruoyi.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.entity.SysPost;
import com.ruoyi.service.SysPostServiceImpl;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 岗位信息操作处理
 */
@RestController
@RequestMapping("/system/post")
public class SysPostController extends BaseController {
    @Autowired
    private SysPostServiceImpl postService;

    /**
     * 获取岗位列表
     */
    @RequestMapping(value = "/list", name = "获取岗位列表")
    public Object selectPage(Page page, @RequestParam Map<String, Object> param) {
        postService.selectPage(page, param);
        return page;
    }

    @PostMapping("/export")
    public void export(HttpServletResponse response, SysPost post) {
        List<SysPost> list = postService.selectPostList(post);
        ExcelUtil<SysPost> util = new ExcelUtil<SysPost>(SysPost.class);
        util.exportExcel(response, list, "岗位数据");
    }

    /**
     * 根据岗位编号获取详细信息
     */

    @GetMapping(value = "/{postId}")
    public AjaxResult getInfo(@PathVariable Long postId) {
        return success(postService.selectPostById(postId));
    }

    @RequestMapping(value = "/getById", name = "岗位详情")
    public Object getById(Long postId) {
        return postService.selectPostById(postId);
    }

    @RequestMapping(value = "/doSave", name = "岗位修改")
    public Object doSave(@RequestBody SysPost post) {
        postService.doSave(post);
        return "";
    }

    @RequestMapping(value = "/deleteById",name = "岗位删除")
    public Object deleteById(Long id) {
        postService.deleteById(id);
        return "";
    }

    @GetMapping(value = "/options",name = "获取岗位选择框列表")
    public Object options() {
        List<SysPost> posts = postService.selectPostAll();
        return posts;
    }

    @RequestMapping(value = "/updateUse", name = "岗位停用启用")
    @Transactional(rollbackFor = Exception.class)
    public Object updateUse(Long id) {
        postService.updateUse(id);
        return "";
    }
}
